package com.qzsoft.zhjd.server.modules.ludong.exp.controller;

import cn.hutool.core.convert.Convert;
import com.qzsoft.zhjd.server.common.utils.MapToBeanUtils;
import com.qzsoft.zhjd.server.common.utils.R;
import com.qzsoft.zhjd.server.modules.ludong.exp.entity.CollectLaboratoryDeviceEntity;
import com.qzsoft.zhjd.server.modules.ludong.exp.entity.CollectPlatformLaboratoryDataEntity;
import com.qzsoft.zhjd.server.modules.ludong.exp.service.CollectExpMarshallStabilityService;
import com.qzsoft.zhjd.server.modules.ludong.exp.entity.CollectExpMarshallStabilityEntity;
import com.qzsoft.zhjd.server.modules.ludong.exp.service.CollectLaboratoryDeviceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.*;


/**
 *
 *功能描述：马歇尔稳定度试验
 * @author jiangyafei
 * @email jiangyf_sd@163.com
 * @date 2020-06-09 11:17:30
 */
@Slf4j
@RestController
@RequestMapping("datacollect/exp/expmarshallstability")
public class CollectExpMarshallStabilityController {
    @Autowired
    private CollectExpMarshallStabilityService collectExpMarshallStabilityService;
    @Autowired
    private CollectLaboratoryDeviceService deviceService;


    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody Map<String,Object> params){
        log.info("马歇尔稳定度试验==="+params.toString());
        try{
            Map<String,Object> map = MapToBeanUtils.toReplaceKeyLow(params);
            CollectExpMarshallStabilityEntity collectExpMarshallStabilityEntity = MapToBeanUtils.mapToObject(map, CollectExpMarshallStabilityEntity.class);
            if (StringUtils.isNotBlank(collectExpMarshallStabilityEntity.getId())){
                collectExpMarshallStabilityService.insertAndRemoveData(collectExpMarshallStabilityEntity);
                //转换到新的表里
                CollectPlatformLaboratoryDataEntity convert = Convert.convert(CollectPlatformLaboratoryDataEntity.class, collectExpMarshallStabilityEntity);
                collectExpMarshallStabilityService.saveMarshall(convert);
                return R.ok();
            }
            return R.error(400,"数据为空或id为空！");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return R.error(400,e.getMessage());
        }

    }

    @PostMapping("/insertMarshall")
    public R insertMarshall(@RequestBody Map map)throws Exception{
        CollectPlatformLaboratoryDataEntity bean = MapToBeanUtils.mapToCapitalObject(map, CollectPlatformLaboratoryDataEntity.class);
        bean.setAverage(map.get("FLOWAVG").toString());
        bean.setStabilityAverage(map.get("AVERAGE").toString());
        bean.setTestType("MXEWDD");
        bean.setEquipmentNumber(map.get("DEVICENO").toString());
        //查询设备编号
        QueryWrapper<CollectLaboratoryDeviceEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("unique_code",map.get("DEVICENO").toString());
        List<CollectLaboratoryDeviceEntity> list = deviceService.list(queryWrapper);
        if (list.size()>0) {
            bean.setDeviceId(list.get(0).getCode());
            bean.setDeptId(list.get(0).getBaseCode());
            bean.setDeptPkid(list.get(0).getDeptPkid());
        }
        bean.setUploadTime(LocalDateTime.now());

        collectExpMarshallStabilityService.saveMarshall(bean);
        return R.ok();
    }


}
